<?php
namespace app\wx\controller;
use think\facade\Request;
use app\wx\validate\AdminValidate;
use app\wx\service\AdminService;

//管理员
class Admin
{
    //查询
    public function findAdmin()
    {
        $page = Request::post('page/d', 1); //当前页
        $limit = Request::post('limit/d', 10); //每页数量
        $field = Request::post('field/s', "*"); //寻找字段
        //接收参数post,查询条件待定
        $param = array(
            'id'     => Request::post('id/s',''), //id
            'name'   => Request::post('name/s',''), //名字
            'number' => Request::post('number/s',''), //账号
            'phone'  => Request::post('phone/s',''), //手机
            'email'  => Request::post('email/s',''), //邮箱
            'status' => Request::post('status/d',''), //状态
            'start_time' => Request::post('startTime/s', ''), //开始时间
            'end_time' => Request::post('entTime/s', '') //结束时间
        );
        $data = AdminService::findAdmin($param,$page,$limit,$field);
        return success($data);
    }

    //添加接口
    public function add()
    {
        //接受参数
        $param = array(
            'name'   => Request::post('name/s',''), //名字
            'number' => Request::post('number/s',''), //账号
            'phone'  => Request::post('phone/s',''), //手机
            'email'  => Request::post('email/s',''), //邮箱
            'status' => Request::post('status/d',1), //状态
            'pwd'    => Request::post('pwd/s',''), //密码
            'remark' => Request::post('remark/s',''), //备注
        );
        $validate = new AdminValidate();
        if (!$validate->scene('add')->check($param)) {
            error($validate->scene('add')->getError());
        }
        $id = AdminService::add($param);
        return success([
            'id' => $id
        ]);
    }

    //修改接口
    public function edit()
    {
        //接受参数
        $param = array(
            'id'     => Request::post('id/d',''), //id
            'name'   => Request::post('name/s',''), //名字
            'phone'  => Request::post('phone/s',''), //手机
            'email'  => Request::post('email/s',''), //邮箱
            'remark' => Request::post('remark/s',''), //备注
        );
        $validate = new AdminValidate();
        if (!$validate->scene('edit')->check($param)) {
            error($validate->scene('add')->getError());
        }
        AdminService::edit($param);
        return success();
    }
}
